package com.xe;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xe.core.XEConstants;
import com.xe.core.XEResource;


import com.xe.core.XEngine;
import com.xe.gae.XEConfigFactory;

@SuppressWarnings("serial")
public class XEServlet extends HttpServlet {
	
	ConfigFactory configfactory;
	
	private String  serviceScript;
	private Map<String, String> configMap ;

	private String getResourceManagerScript;
	
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		
		Engine engine = initEngine(req, resp);
		XEResource r =null;

	
		engine.run(serviceScript);
		
		resp.setContentType("text/html");

	}


	private Engine  initEngine(HttpServletRequest req,HttpServletResponse resp) {

		Engine engine   = new XEngine();
		engine.setVariable(XEConstants.VAR_HTTP_REQUEST,req);
		
		engine.setVariable(XEConstants.VAR_HTTP_RESPONSE,resp);
		engine.setVariable(XEConstants.VAR_SESSION,req.getSession());

		ResourceManager resourceManager = (ResourceManager)engine.runScript(getResourceManagerScript);
		
		engine.setVariable(XEConstants.VAR_XE_RESOURCE_MANAGER,resourceManager);
		//todo setear el entorno para la engine;
		
		return engine;
	}

	
	
	@Override
	public void init() throws ServletException {
		try {
			config();

		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		super.init();
	}



	private void config() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		String cfgClassName=this.getServletContext().getInitParameter(XEConstants.XE_CONFIG_FACTORY_CLASS);
		Class c = Class.forName(cfgClassName);
		configfactory  = (XEConfigFactory) c.newInstance();
		configfactory.setContext(this.getServletContext());
		this.configMap = configfactory.getConfig();

		getResourceManagerScript = configMap.get(XEConstants.XE_GET_RESOURCE_MANAGER_SCRIPT);
		
		serviceScript = configMap.get(XEConstants.SERVICE_SCRIPT);
		
		
		Engine engine   = new XEngine();
		ResourceManager resourceManager = (ResourceManager)engine.runScript(getResourceManagerScript);

		XEResource resource = resourceManager.load(serviceScript);
		
		if(resource==null){
			String moduleName=serviceScript.split("/")[0];
			XEResource module=new XEResource(moduleName,XEResource.FOLDER);
			XEResource script=new XEResource(serviceScript,XEResource.FILE);
			script.setSource("resp.getWriter().write('XE IS RUNNING!!!!')");
			resourceManager.save(module);
			resourceManager.save(script);
			
			
		}

		
		
	}

	


	
	
}
 